% nassi.sty  29 augustus 1989, C.M. Lindhout.
%
%  Copyright (C) by CAWCS, Royal Netherlands Navy.
%  This program may be freely used and copied but may not be sold
%  without the author's written permission.  This notice must remain
%  in any copy or derivative.
%
%  Please send improvements, bug fixes, comments, etc., to
%    Marion van Geest,
%    CAWCS,
%    Royal Netherlands Navy,
%    P.O.BOX 10000,
%    1780 CA Den Helder,
%    The Netherlands.

%  Modifications.
%  --------------
% Version 2, july 1990,
%  Scope of \setiftext command changed
% Version 3, november 1990,
%  A lot of unnecessary 'overfull hbox' messages are no longer given
% Version 4, october 1991,
%  \ACCEPT created for use with ADA code
% Version 5, august 1992,
%  Improved layout for empty \ELSE in \IF statement

\typeout{Nassi-Scheiderman macro's.}

\global\newdimen\wwa \global\newdimen\wwb \global\newdimen\wwc
\global\newdimen\wwd \global\newdimen\wwe \global\newdimen\wwf
\global\newdimen\wwg \global\newdimen\wwh \global\newdimen\wwi
\global\newdimen\wwj \global\newdimen\wwk \global\newdimen\wwl
\global\newdimen\wwm \global\newdimen\wwn \global\newdimen\wwo
\global\newdimen\wwp \global\newdimen\wwq \global\newdimen\wwr
\global\newdimen\wws \global\newdimen\wwt \global\newdimen\wwu
\global\newdimen\wwv \global\newdimen\www \global\newdimen\wwx
\global\newdimen\wwy \global\newdimen\wwz
\global\newdimen\wwA \global\newdimen\wwB \global\newdimen\wwC
\global\newdimen\wwD \global\newdimen\wwE \global\newdimen\wwF
\global\newdimen\wwG \global\newdimen\wwH \global\newdimen\wwI
\global\newdimen\wwJ \global\newdimen\wwK \global\newdimen\wwL
\global\newdimen\wwM \global\newdimen\wwN \global\newdimen\wwO
\global\newdimen\wwP \global\newdimen\wwQ \global\newdimen\wwR
\global\newdimen\wwS \global\newdimen\wwT \global\newdimen\wwU
\global\newdimen\wwV \global\newdimen\wwW \global\newdimen\wwX
\global\newdimen\wwY \global\newdimen\wwZ

\def\ww#1{%
  \ifcase#1%
    \wwa\or\wwb\or\wwc\or\wwd\or\wwe\or\wwf\or\wwg\or\wwh\or\wwi\or\wwj\or
    \wwk\or\wwl\or\wwm\or\wwn\or\wwo\or\wwp\or\wwq\or\wwr\or\wws\or\wwt\or
    \wwu\or\wwv\or\www\or\wwx\or\wwy\or\wwz\or\wwA\or\wwB\or\wwC\or\wwD\or
    \wwE\or\wwF\or\wwG\or\wwH\or\wwI\or\wwJ\or\wwK\or\wwL\or\wwM\or\wwN\or
    \wwO\or\wwP\or\wwQ\or\wwR\or\wwS\or\wwT\or\wwU\or\wwV\or\wwW\or\wwX\or
    \wwY\or\wwZ
  \fi%
}%

\global\newcount\nr%
\global\newif\iftry%
\global\newif\ifunderline%
\newif\ifunderlinethen%
\newif\ifunderlineelse%
\newif\ifunderlinewhen%
\underlinewhenfalse%
\newdimen\nassiwidth%
\nassiwidth=0pt%
\newdimen\minunitwidth%
\minunitwidth=60pt%
\newdimen\unitwidth%
\newdimen\thickness%
\newbox\thentext%
\newbox\elsetext%
\thickness=0.01in%
\newdimen\spc%
\newdimen\fac%
\newdimen\ha%
\newdimen\hb%
\newdimen\hc%
\newcount\nra%
\newcount\nrb%
\newdimen\wa%
\newdimen\wb%
\newdimen\wc%
%
\def\reserveww#1{#1=\nr \global\advance\nr by 1}%
\def\saveww#1#2{\global\ww#2=#1}%
\def\readww#1{#1=\ww\nr \global\advance\nr by 1}%
%
\def\setiftext#1#2{\setbox\thentext=\hbox{#1}\setbox\elsetext=\hbox{#2}}%
\setiftext{Y}{N}%
%
\def\vspc{%
  \vbox to 2\spc{}%
}%
%
\def\hspc{%
  \hbox to \spc{}%
}%
%
\def\spcbox#1#2{%
  \hbox{%
    \hspc%
    \vbox{%
      \vspc%
      \wa=#1%
      \advance\wa-2\spc%
      \hbox{\parbox[t]{\wa}{\raggedright#2}}%
      \vspc%
    }%
    \hspc%
  }%
}%
%
\def\ACTION#1{%
  \iftry%
    \hbox to \unitwidth{}%
  \else%
    \hrule height\thickness%
    \spcbox{\unitwidth}{#1}%
    \global\underlinefalse%
  \fi%
}%
%
\def\PROC#1#2{%
  \iftry%
    \hbox to \unitwidth{}%
  \else%
    \hrule height\thickness%
    \spcbox{\unitwidth}{#2 (#1)}%
    \global\underlinefalse%
  \fi%
}%
%
\def\LABEL#1{%
}%
%
\def\leftif{%
  \hbox{%
    \begin{picture}(80,60)%
      \put(0,60){\line(4,-3){80}}%
      \put(0,5){\hbox to 80\unitlength{\hspc\copy\thentext\hfil}}%
    \end{picture}%
  }%
}%
%
\def\rightif{%
  \hbox{%
    \begin{picture}(80,60)%
      \put(0,0){\line(4,3){80}}%
      \put(0,5){\hbox to 80\unitlength{\hfil\copy\elsetext\hspc}}%
    \end{picture}%
  }%
}%
%
\def\IF#1\THEN#2\ELSE#3\ENDIF{%
  \iftry%
    \reserveww{\nra}%
    \reserveww{\nrb}%
    \setbox0=\vbox{\hbox to 8ex{} #2}%
    \setbox2=\vbox{\hbox to 8ex{} #3}%
    \saveww{\wd0}{\nra}%
    \saveww{\wd2}{\nrb}%
    \hbox{\hbox to 16ex{}\hbox to \unitwidth{}}%
    \hbox{\box0\box2}%
  \else%
    \readww{\wa}%
    \readww{\wb}%
%
    \ifdim \wb=8ex%
	\wa=\unitwidth%
	\advance\wa-\wb%
    \else%
	\wc=0.003\wa%
	\advance\wc0.003\wb%
	\wb=\unitwidth%
	\divide\wb\wc%
	\wa=0.003\wa%
	\multiply\wa\wb%
%
	\wb=\unitwidth%
	\advance\wb-\wa%
    \fi%
%
    \hrule height\thickness%
    \hbox{%
      \leftif%
      \wa=\unitwidth%
      \advance\wa-16ex%
      \spcbox{\wa}{#1}%
      \rightif%
    }%
    \setbox0=\vbox{\unitwidth=\wa\hbox to \unitwidth{}\global\underlinetrue #2}%
    \ifunderline\underlinethentrue\else\underlinethenfalse\fi%
    \ha=\ht0%
    \advance\ha\dp0%
    \setbox2=\vbox{\unitwidth=\wb\hbox to \unitwidth{}\global\underlinetrue #3}%
    \ifunderline\underlineelsetrue\else\underlineelsefalse\fi%
    \hb=\ht2%
    \advance\hb\dp2%
    \ifdim\ha>\hb\hc=\ha\else\hc=\hb\fi%
    \hbox{%
      \vtop{%
        \unvbox0%
        \ifdim\ha<\hb{\ifunderlinethen\hrule height\thickness\fi}\fi%
      }%
      \hbox to -\thickness{}%
      \vrule width\thickness%
      \vtop{%
        \unvbox2%
        \ifdim\hb<\ha{\ifunderlineelse\hrule height\thickness\fi}\fi%
      }%
    }%
    \global\underlinetrue%
  \fi%
}%
%
\def\REPEAT#1\UNTIL#2{%
  \iftry%
    \hbox{%
      \hbox to 4ex{}%
      \vtop{%
        #1%
      }%
    }%
    \hbox to \unitwidth{}%
  \else%
    \hbox{%
      \vtop{%
        \hrule height\thickness%
        \hbox to 4ex{}%
      }%
      \vrule width\thickness%
      \vtop{%
        \wa=\unitwidth%
        \advance\wa-4ex%
        \unitwidth=\wa%
        #1%
        \hrule height\thickness%
      }%
    }%
    \spcbox{\unitwidth}{#2}%
    \global\underlinetrue%
  \fi%
}%
%
\def\WHILE#1#2\ENDWHILE{%
  \iftry%
    \hbox to \unitwidth{}%
    \hbox{%
      \hbox to 4ex{}%
      \vtop{%
        #2%
      }%
    }%
  \else%
    \hrule height\thickness%
    \spcbox{\unitwidth}{#1}%
    \hbox{%
      \hbox to 4ex{}%
      \vrule width\thickness%
      \vbox{%
        \wa=\unitwidth%
        \advance\wa-4ex%
        \unitwidth=\wa%
        #2%
      }%
    }%
    \global\underlinetrue%
  \fi%
}%
%
\def\ACCEPT#1#2\ENDACCEPT{%
  \iftry%
    \hbox to \unitwidth{}%
    \hbox{%
      \hspc%
      \vtop{%
        #2%
      }%
      \hspc%
    }%
  \else%
    \hrule height\thickness%
    \spcbox{\unitwidth}{#1}%
    \hbox{%
      \hspc%
      \vbox{%
        \hbox{%
          \vrule width\thickness%
          \hbox to -\thickness{}%
          \vbox{%
            \wa=\unitwidth%
            \advance\wa-2\spc%
            \unitwidth=\wa%
            #2%
          }%
          \hbox to -\thickness{}%
          \vrule width\thickness%
        }%
        \vbox to -\thickness{}%
        \hrule height\thickness%
        \vbox to \spc{}%
      }%
      \hspc%
    }%
    \global\underlinetrue%
  \fi%
}%
%
\def\WHEN#1#2{%
  \iftry%
    \reserveww{\nra}%
    \setbox0=\vbox{\hbox to \unitwidth{}#2}%
    \saveww{\wd0}{\nra}%
    \box0%
  \else%
    \readww{\wa}%
    \wa=0.003\wa%
    \multiply\wa\wc%
    \vtop{%
      \global\underlinefalse%
      \hbox to 0pt{}%
      \unitwidth=\wa%
      \setbox0=\hbox{\raggedright#1}%
      \ifdim\wd0=0pt%
        #2%
      \else%
        \hrule height\thickness%
        \spcbox{\unitwidth}{#1}%
        #2%
      \fi%
      \ifunderlinewhen\ifunderline\hrule height\thickness\fi\fi%
    }%
    \hbox to -\thickness{}%
    \vrule width\thickness%
  \fi%
}%
%
\def\leftcase{%
  \hbox{%
    \begin{picture}(80,60)%
      \put(0,60){\line(4,-3){80}}%
    \end{picture}%
  }%
}%
%
\def\CASE#1#2\ENDCASE{%
  \iftry%
    \reserveww{\nra}%
    \setbox0=\hbox{#2}%
    \saveww{\wd0}{\nra}%
    \hbox{\box0}%
  \else%
    \readww{\wa}%
%
    \wc=0.003\wa%
    \wb=\unitwidth%
    \divide\wb\wc%
    \wc=\wb%
%    \wa=0.01\wa%
%    \multiply\wa\wb%
%
    \hrule height\thickness%
    \hbox{%
      \leftcase%
      \wa=\unitwidth%
      \advance\wa-8ex%
      \spcbox{\wa}{#1}%
    }%
    \hbox{#2}%
    \global\underlinetrue%
  \fi%
}%
%
\def\STRUCT#1#2#3{%
  \spc=1ex%
  \unitlength=0.1\spc%
%
  \nr=0%
  \trytrue%
  \unitwidth=\minunitwidth%
  \setbox0=\vbox{%
    \baselineskip=0pt%
    \lineskip=0pt%
    \parindent=0pt%
    \parskip=0pt%
    #3
  }%
%
  \nr=0%
  \tryfalse%
  \ifdim\nassiwidth=0pt%
    \ifdim\wd0>\textwidth%
      \unitwidth=\textwidth%
    \else%
      \unitwidth=\wd0%
    \fi%
  \else%
    \unitwidth=\nassiwidth%
  \fi%
  \vbox{%
    \vspace{0.5cm}
    {\normalsize{\bf #1 --- }#2}%
    \vspace{0.5cm}
    \par%
    \hbox{%
      \baselineskip=0pt%
      \lineskip=0pt%
      \parindent=0pt%
      \parskip=0pt%
      \vrule width\thickness%
      \vbox{%
        #3%
        \hrule height\thickness%
      }%
      \hbox to -\thickness{}%
      \vrule width\thickness%
    }%
  }%
}%
